package com.gxan.web.renshi;

import com.alibaba.fastjson.JSONObject;
import com.gxan.pojo.Archives;
import com.gxan.renshiservice.ServiceArchives;
import com.gxan.renshiservice.impl.ServiceArchivesImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/archives/update")
public class UpdateArchivesServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        //从传来的数据中读取一行
        String params = req.getReader().readLine();
        //将读取的数据传递给emp对象
        Archives archives = JSONObject.parseObject(params, Archives.class);
        ServiceArchives serviceArchives = new ServiceArchivesImpl();
        archives.setDept_id(Integer.valueOf(archives.getDept_full_name()));
        System.out.println("name"+archives.getSal_standard_name());
        //传数据用map
        Map map = new HashMap();
        try {
            int result = serviceArchives.updateArchives(archives);
            if (result>0){
                map.put("code",0);
                map.put("msg","修改成功");
                //******写数据json到前端页面显示对应信息
                resp.getWriter().write(JSONObject.toJSONString(map));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","修改失败，出现错误或异常");
            //******写数据json到前端页面，显示对应信息
            resp.getWriter().write(JSONObject.toJSONString(map));
        }
        resp.getWriter().flush();
        resp.getWriter().close();
    }
    }
